#include "CVipMgr.h"

#include <iostream>
#include <fstream>
#include "pb_cfg_vip.pb.h"
#include "../mainctrl.h"

int CVipMgr::Init()
{
    int iRet = 0;

    iRet = _stVipConfig.Init();
    CHECK_RETURN(iRet);

    return 0;
}

int CVipMgr::LoadConfig(const char* szLogPath)
{
    int iRet = 0;

    if (szLogPath == NULL)
    {
        CT_ERROR(("vip Log Path is null"));
        return RET_SYSTEM_FAIL;
    }

    pb_cfg_vip  stvipPBConfig;
    std::fstream stFileInput(szLogPath, std::ios::in | std::ios::binary);
    if (!stvipPBConfig.ParseFromIstream(&stFileInput))
    {
        CT_ERROR(("Failed to Parse vipCfg!"));
        return RET_SYSTEM_FAIL;
    }

    //解析配置文件内容
    CT_TRACE(("vip config count %d", stvipPBConfig.giftaward_config_rows_size()));
    for (int i = 0; i < stvipPBConfig.giftaward_config_rows_size(); ++i)
    {
        const pb_cfg_vip_pb_cfg_giftaward_config_unit& stvipPBConfigUnit
            = stvipPBConfig.giftaward_config_rows(i);

        CVipAwardConfigData stConfigData;

        int iGiftType = stvipPBConfigUnit.gift_type();
        stConfigData._iVipLevel = stvipPBConfigUnit.vip_level();

        CVipAwardConfigData::TAward tba;
        int iAwardSeq = stvipPBConfigUnit.award_seq();
        tba.iAwardItemType = stvipPBConfigUnit.award_item_type();
        tba.iAwardItemId   = stvipPBConfigUnit.award_item_id();
        tba.iAwardNum      = stvipPBConfigUnit.award_num();

        stConfigData._aTAwards.AddOneItem( tba );

        stConfigData._bHasConfig = true;

        //每日礼包配置
        if( iGiftType==CONST_VIP_DAILY_GIFT )
        {
            iRet = this->_stVipConfig.AddVipDailyAwardConfig( stConfigData );
        }
        else if( iGiftType==CONST_VIP_LEVEL_GIFT) //等级礼包
        {
            iRet = this->_stVipConfig.AddVipLevelAwardConfig( stConfigData );
        }
        else
        {
            CT_WARNING(("ERROR vip gift type %d config!", iGiftType));
            continue;
        }

        CHECK_RETURN(iRet);
        CT_TRACE(("礼包奖励类型%d:级别%d, 第%d个奖励: 类型:%d,id:%d, 数量:%d",
            iGiftType, stConfigData._iVipLevel, iAwardSeq,
            tba.iAwardItemType, tba.iAwardItemId, tba.iAwardNum));
    }

    //解析金币充值配置
    for (int i = 0; i < stvipPBConfig.vip_pay_config_rows_size(); ++i)
    {
        const pb_cfg_vip_pb_cfg_vip_pay_config_unit& stvipPBConfigUnit
            = stvipPBConfig.vip_pay_config_rows(i);

        CVipPayGoldConfig stConfigData;

        stConfigData._iVipLevel = stvipPBConfigUnit.level();
        stConfigData._iNeedGold = stvipPBConfigUnit.pay_gold();

        stConfigData._bHasConfig = true;

        iRet = this->_stVipConfig.AddVipPayGoldConfig( stConfigData );

        CT_TRACE(("充值金币:级别%d, 金币数量:%d", stConfigData._iVipLevel, stConfigData._iNeedGold ));
        CHECK_RETURN(iRet);
    }

    //vip付费目标次数配置
    for (int i = 0; i < stvipPBConfig.vip_ext_num_rows_size(); ++i)
    {
        const pb_cfg_vip_pb_cfg_vip_ext_num_unit& stvipPBConfigUnit
            = stvipPBConfig.vip_ext_num_rows(i);

        CVipExtNumConfig stConfigData;

        stConfigData._iModuleId = stvipPBConfigUnit.id();
        stConfigData._iVipLevel = stvipPBConfigUnit.vip_level();
        stConfigData._iModuleExtNum = stvipPBConfigUnit.ext_num();

        stConfigData._bHasConfig = true;

        iRet = this->_stVipConfig.AddVipExtNumConfig( stConfigData );

        CHECK_RETURN(iRet);
    }

    //黄钻礼包配置
    {
        if (stvipPBConfig.yellow_gift_rows_size() != 1)
        {
            CT_ERROR((""));
            return RET_SYS_ERROR;
        }

        const pb_cfg_vip_pb_cfg_yellow_gift_unit& stvipPBConfigUnit
            = stvipPBConfig.yellow_gift_rows(0);

        const std::string sDel = ",";

        //每日礼包
        std::string s = stvipPBConfigUnit.day_gift_id();
        std::vector<std::string> vIDs;

        CTStringUtils::Split(s, sDel, vIDs);
        for (unsigned int i = 0; i < vIDs.size(); ++i)
        {
            iRet = _stYellowGiftConfig.SetDayGiftID(i + 1, atoi(vIDs[i].c_str()));
            CHECK_RETURN(iRet);
        }

        //年费每日礼包
        s = stvipPBConfigUnit.year_day_gift_id();
        vIDs.clear();
        CTStringUtils::Split(s, sDel, vIDs);
        for (unsigned int i = 0; i < vIDs.size(); ++i)
        {
            iRet = _stYellowGiftConfig.SetYearDayGiftID(i + 1, atoi(vIDs[i].c_str()));
            CHECK_RETURN(iRet);
        }

        //等级礼包
        s = stvipPBConfigUnit.level_gift_id();

        vIDs.clear();
        CTStringUtils::Split(s, sDel, vIDs);
        for (unsigned int i = 0; i < vIDs.size(); ++i)
        {
            iRet = _stYellowGiftConfig.SetLevelGiftID(i + 1, atoi(vIDs[i].c_str()));
            CHECK_RETURN(iRet);
        }

        //新手礼包
        _stYellowGiftConfig.SetNewbieGiftID(stvipPBConfigUnit.newbie_gift_id());

        _stYellowGiftConfig._usYellowMount = stvipPBConfigUnit.yellow_mount();
        if (false == CMainCtrl::m_pstMain->m_stMountMgr.HasThisMount(_stYellowGiftConfig._usYellowMount))
        {
            CT_ERROR(("%d mount don't exist", _stYellowGiftConfig._usYellowMount));
            return ERROR_VIP_MOUNT_CONFIG;
        }
    }

    //蓝钻礼包配置
    {
        if (stvipPBConfig.blue_gift_rows_size() != 1)
        {
            CT_ERROR((""));
            return RET_SYS_ERROR;
        }

        const pb_cfg_vip_pb_cfg_blue_gift_unit& stvipPBConfigUnit
            = stvipPBConfig.blue_gift_rows(0);

        const std::string sDel = ",";

        //每日礼包
        std::string s = stvipPBConfigUnit.day_gift_id();
        std::vector<std::string> vIDs;

        CTStringUtils::Split(s, sDel, vIDs);
        for (unsigned int i = 0; i < vIDs.size(); ++i)
        {
            iRet = _stBlueGiftConfig.SetDayGiftID(i + 1, atoi(vIDs[i].c_str()));
            CHECK_RETURN(iRet);
        }

        //年费每日礼包
        s = stvipPBConfigUnit.year_day_gift_id();
        vIDs.clear();
        CTStringUtils::Split(s, sDel, vIDs);
        for (unsigned int i = 0; i < vIDs.size(); ++i)
        {
            iRet = _stBlueGiftConfig.SetYearDayGiftID(i + 1, atoi(vIDs[i].c_str()));
            CHECK_RETURN(iRet);
        }

        //等级礼包
        s = stvipPBConfigUnit.level_gift_id();

        vIDs.clear();
        CTStringUtils::Split(s, sDel, vIDs);
        for (unsigned int i = 0; i < vIDs.size(); ++i)
        {
            iRet = _stBlueGiftConfig.SetLevelGiftID(i + 1, atoi(vIDs[i].c_str()));
            CHECK_RETURN(iRet);
        }

        //新手礼包
        _stBlueGiftConfig.SetNewbieGiftID(stvipPBConfigUnit.newbie_gift_id());

        //豪华礼包
        _stBlueGiftConfig._iSuperVipGiftID = atoi(stvipPBConfigUnit.super_day_gift_id().c_str());

        //3366礼包配置
        s = stvipPBConfigUnit.p3366_day_gift_id();

        vIDs.clear();
        CTStringUtils::Split(s, sDel, vIDs);
        if (vIDs.size() != (unsigned int)MAX_3366_LEVEL_COUNT)
        {
            CT_ERROR(("%d", vIDs.size()));
            return RET_SYS_ERROR;
        }

        for (unsigned int i = 0; i < vIDs.size(); ++i)
        {
            _stBlueGiftConfig._ast3366DayGiftID.AddOneItem(atoi(vIDs[i].c_str()));
        }

        _stBlueGiftConfig._usBlueMount = stvipPBConfigUnit.blue_mount();
        if (false == CMainCtrl::m_pstMain->m_stMountMgr.HasThisMount(_stBlueGiftConfig._usBlueMount))
        {
            CT_ERROR(("%d mount don't exist", _stBlueGiftConfig._usBlueMount));
            return ERROR_VIP_MOUNT_CONFIG;
        }
    }

    return 0;
}

int CVipMgr::GetVipInfo(CPlayerBaseInfo& stBaseInfo, MsgAnsVipInfo& stAns)
{
    int iRet = 0;
    CVipData & sVipData = stBaseInfo._stVipData;
    int _uiGid = stBaseInfo._uiGID;

    stAns.set_gid( _uiGid );
    stAns.set_vip_level( stBaseInfo._chVipLevel );
    stAns.set_pay_gold( sVipData._iPayGold );
    stAns.set_ret( RET_OK );
    stAns.set_has_first_pay_bonus(sVipData._ucCanGetFirstPayBonus);

    return iRet;
}

int CVipMgr::GetVipGiftInfo(CPlayerBaseInfo& stBaseInfo, int iGiftType, MsgAnsVipGiftInfo& stAns)
{
    int iRet = 0;
    bool bHasUpdate = false; //是否需要更新数据入库

    CVipData & sVipData = stBaseInfo._stVipData;

    //检查是否已经初始化过礼包
    if( stBaseInfo._chVipLevel >= 1 && !sVipData._bHasInit )
    {
        sVipData.Clear();
        sVipData._aVipLevelGift.m_iUsedCount = sVipData._aVipLevelGift.GetCount();
        for( int i=0; i<sVipData._aVipLevelGift.Size(); ++i )
        {
            sVipData._aVipLevelGift[i] = CONST_VIP_GIFT_NONE;
        }
        sVipData._bHasInit = 1; //设置为已经初始化过
        bHasUpdate = true;
    }

    if( iGiftType==CONST_VIP_DAILY_GIFT)//每日礼包
    {
        if( stBaseInfo._chVipLevel<1 )
        {
            iRet = ERROR_NOT_VIP_USER;
            return iRet;
        }

        int iLeftCounts = sVipData._sVipDailyAward.Get( CMainCtrl::m_pstMain->m_iNow, 0 );
        MsgGiftStatusOne* mGiftOne = stAns.add_gift_status();

        mGiftOne->set_id(0);

        if( iLeftCounts<1 )
        {
            mGiftOne->set_status(CONST_VIP_GIFT_OPENED);
        }
        else
        {
            mGiftOne->set_status(CONST_VIP_GIFT_CAN_OPEN);
        }
    }
    else if( iGiftType==CONST_VIP_LEVEL_GIFT )//等级礼包
    {
        if( stBaseInfo._chVipLevel<1 )
        {
            iRet = ERROR_NOT_VIP_USER;
            return iRet;
        }

        //判断vip级别能领取哪些
        for( int i=1; i<=stBaseInfo._chVipLevel; ++i )
        {
            if( sVipData._aVipLevelGift[i]==CONST_VIP_GIFT_NONE )
            {
                sVipData._aVipLevelGift[i] = CONST_VIP_GIFT_CAN_OPEN;

                bHasUpdate = true;
            }
        }

        for( int i=1; i<MAX_VIP_LEVEL; ++i )
        {
            MsgGiftStatusOne* mGiftOne = stAns.add_gift_status();
            mGiftOne->set_id(i);
            mGiftOne->set_status( sVipData._aVipLevelGift[i] );
        }
    }
    else if (CONST_VIP_FIRST_PAY == iGiftType)
    {
        stAns.set_can_get_first_pay_gift(sVipData._ucCanGetFirstPayBonus);
    }
    else
    {
        iRet = ERROR_VIP_OPERATE_TYPE;
    }

    // 入库
    if( bHasUpdate )
    {
        CVarObjectKey stKey;
        stKey.m_chDataType = DT_PLAYER_BASE_INFO;
        stKey.m_u64ID = stBaseInfo._uiGID;
        CMainCtrl::m_pstMain->UpdateData(stKey, stBaseInfo, VOP_UPDATE);
    }

    return iRet;
}

int CVipMgr::GetVipGiftOpen(CPlayerBaseInfo& stBaseInfo, CPlayerDetailInfo& stPlayerDetail, CPlayerDetailInfoEx1& stPlayerDetailEx1, int iGiftId, int iGiftType, MsgAnsOpenGift& stAns)
{
    int iRet = 0;
    bool bHasUpdate = false; //是否需要更新数据入库
    bool bUpdateDetail = false; //是否需要更新Detail入库

    CVipData & sVipData = stBaseInfo._stVipData;

    if( iGiftType==CONST_VIP_DAILY_GIFT)//每日礼包
    {
        if( stBaseInfo._chVipLevel<1 )
        {
            iRet = ERROR_NOT_VIP_USER;
            return iRet;
        }

        int iLeftCounts = sVipData._sVipDailyAward.Get( CMainCtrl::m_pstMain->m_iNow, 0 );

        if( iLeftCounts<1 )
        {
            iRet = ERROR_ALREADY_OPEN_DAILY_GIFT;
        }
        else
        {
            //给予礼包奖励
            iRet=this->TriggerGiftAward(stBaseInfo._uiGID, stBaseInfo._chVipLevel, iGiftType, stAns);
            //奖励成功，扣取任务数量
            if( iRet==RET_OK )
            {
                sVipData._sVipDailyAward.Sub(1, CMainCtrl::m_pstMain->m_iNow, 0);
                bHasUpdate = true;
            }
        }
    }
    else if( iGiftType==CONST_VIP_LEVEL_GIFT)//等级礼包
    {
        if( stBaseInfo._chVipLevel<1 )
        {
            iRet = ERROR_NOT_VIP_USER;
            return iRet;
        }

        if( iGiftId<1 || iGiftId>=MAX_VIP_LEVEL )
        {
            iRet = ERROR_VIP_LEVEL_ID;
            return iRet;
        }

        //检查是否达到领取的级别
        if( iGiftId>stBaseInfo._chVipLevel )
        {
            iRet = ERROR_VIP_LEVEL_LOWER;
            return iRet;
        }

        //判断vip此id级别礼包是否可以领取
        if( sVipData._aVipLevelGift[iGiftId] != CONST_VIP_GIFT_CAN_OPEN)
        {
            iRet = ERROR_VIP_LEVEL_GIFT_HAS_OPEN;
            return iRet;
        }
        //给予礼包奖励
        iRet=this->TriggerGiftAward(stBaseInfo._uiGID, iGiftId, iGiftType, stAns);
        //奖励成功，设置为已经领取
        if( iRet==RET_OK )
        {
            sVipData._aVipLevelGift[iGiftId] = CONST_VIP_GIFT_OPENED;
            bHasUpdate = true;
        }
    }
    else if (CONST_VIP_FIRST_PAY == iGiftType)
    {
        return 0;//首充活动领取礼包的入口关闭 统一走累计消费逻辑 byronwei 20131217

        if (sVipData._ucCanGetFirstPayBonus != 1)
        {
            return ERROR_VIP_NO_FIRST_GIFT;
        }

        int iIndex = CMainCtrl::m_pstMain->m_stUnactPartnerInfoHash.HashFind(stBaseInfo._uiGID);
        if (iIndex < 0)
        {
            CT_ERROR(("no unact partner data user(%u)",stBaseInfo._uiGID));
            return RET_SYS_ERROR;
        }
        CUnactPartnerInfo& stUnactPartnerInfo = CMainCtrl::m_pstMain->m_stUnactPartnerInfoHash[iIndex];

        iIndex = CMainCtrl::m_pstMain->m_stPlayerOnlineDataHash.HashFind(stBaseInfo._uiGID);
        if (iIndex < 0)
        {
            CT_ERROR((""));
            return RET_SYS_ERROR;
        }
        CPlayerOnlineData& stPlayerOnlineData = CMainCtrl::m_pstMain->m_stPlayerOnlineDataHash[iIndex];

        CDropItem stDropItem;
        iRet = CMainCtrl::m_pstMain->m_stItemMgr.GetGiftItem(FIRST_PAY_GIFT_ID, stBaseInfo._uiGID, stDropItem, false);
        CHECK_RETURN(iRet);

        if (CMainCtrl::m_pstMain->m_stItemMgr.CheckBagFull(stPlayerOnlineData._stUserStorage, stDropItem))
        {
            return ERROR_STORAGE_BAG_IS_FULL;
        }

        //奖励伙伴预检查
        short shPartnerID =
            CMainCtrl::m_pstMain->m_stPartnerMgr.GetFirstPayPartner();
        if (shPartnerID <= 0)
        {
            return ERROR_PARTNER_ERROR_CFG;
        }
        bool bRoleGetPartner =
            CMainCtrl::m_pstMain->m_stPartnerMgr.IsRoleGetPartner(shPartnerID,
            stPlayerDetail, stUnactPartnerInfo);
        if (!bRoleGetPartner)
        {
            iRet = CMainCtrl::m_pstMain->m_stPartnerMgr.CheckPartnerJoin(
                shPartnerID, CPartnerMgr::PARTNER_JOIN_TYPE_SYSTEM, stBaseInfo,
                stPlayerDetail, stUnactPartnerInfo, stPlayerOnlineData);
            CHECK_RETURN(iRet);
        }

        //发送伙伴 内部检查是否可以领取首充奖励的标志
        if (!bRoleGetPartner)
        {
            iRet = CMainCtrl::m_pstMain->m_stPartnerMgr.PartnerJoinFirstPay(
                stBaseInfo, stPlayerDetail, stUnactPartnerInfo,
                stPlayerOnlineData, stPlayerDetailEx1);
            if (iRet)
            {
                CT_ERROR(("role(%u) get first pay partner error(%d)",
                    stBaseInfo._uiGID,iRet));
            }
        }

        //设置已经领取
        sVipData._ucCanGetFirstPayBonus = 0;

        //发送物品
        stDropItem.SetOssModule(OSS_MODULE_GIFT);
        CMainCtrl::m_pstMain->m_stItemMgr.DropItem(NULL, stBaseInfo._uiGID, stDropItem);

        //保存数据
        bUpdateDetail = true;//伙伴保存在detail 需要更新入库
        bHasUpdate = true;

        //跑马灯
        MsgTips stTips;
        stTips.set_type(TIPS_TOTAL_CHARGE_STAGE_ONE);
        stTips.add_tip_argument(stBaseInfo._sRoleName());
        CMainCtrl::m_pstMain->SendOnlineTips(stTips);
    }
    else
    {
        iRet = ERROR_VIP_OPERATE_TYPE;
    }

    // 入库
    if( bHasUpdate )
    {
        CVarObjectKey stKey;
        stKey.m_chDataType = DT_PLAYER_BASE_INFO;
        stKey.m_u64ID = stBaseInfo._uiGID;
        CMainCtrl::m_pstMain->UpdateData(stKey, stBaseInfo, VOP_UPDATE);
    }

    if (bUpdateDetail)
    {
        CVarObjectKey stKey;
        stKey.m_chDataType = DT_PLAYER_DETAIL_INFO;
        stKey.m_u64ID = stPlayerDetail._uiGID;
        CMainCtrl::m_pstMain->UpdateData(stKey, stPlayerDetail, VOP_UPDATE);
    }

    if( bHasUpdate )
    {
        NotifyGiftStatus(stBaseInfo, iGiftType);
    }

    return iRet;
}

int CVipMgr::TriggerGiftAward(unsigned int iGID, int iVipLevel, int iGiftType, MsgAnsOpenGift& stAns)
{
    int iRet = 0;
    CDropItem stDropItem;

    //查询奖励配置
    CVipAwardConfigData stConfigData;
    if( iGiftType==CONST_VIP_DAILY_GIFT )
    {
        iRet = this->_stVipConfig.GetVipDailyAwardConfig(iVipLevel, stConfigData);
    }
    else if( iGiftType==CONST_VIP_LEVEL_GIFT )
    {
        iRet = this->_stVipConfig.GetVipLevelAwardConfig(iVipLevel, stConfigData);
    }
    CHECK_RETURN(iRet);

    iRet = this->GetVipAwardItems(iGID, stConfigData, stDropItem);
    if (iRet != RET_OK)
    {
        CT_WARNING(("CVipMgr::TriggerVipAward:get award items fail-%d", iRet));
    }

    stDropItem.GetPBMsg(iGID, *stAns.mutable_drop());

    //给予礼包奖励
    //assign award items to user
    stDropItem.SetOssModule(OSS_MODULE_VIP);
    iRet = CMainCtrl::m_pstMain->m_stItemMgr.DropItem(NULL, iGID, stDropItem);

    return iRet;
}

int CVipMgr::GetVipAwardItems(unsigned int iGID, CVipAwardConfigData& stConfigData, CDropItem& stDropItem)
{
    int iRet = 0;
    CDropItemData stDropItemData;

    for (int i = 0; i < stConfigData._aTAwards.Size(); ++i)
    {//此处不考虑类集的物品奖励
        const CVipAwardConfigData::TAward& OneAward = stConfigData._aTAwards[i];
        CMainCtrl::m_pstMain->m_stItemMgr.GetDropItemData(iGID, OneAward.iAwardItemType,
            OneAward.iAwardItemId, "", COMM_MOD, OneAward.iAwardNum, stDropItemData);

        stDropItem.Drop(stDropItemData);
    }
    return iRet;
}

//通知礼包状态
void CVipMgr::NotifyGiftStatus(CPlayerBaseInfo& stBaseInfo, int iGiftType)
{
    MsgAnsVipGiftInfo stAns;
    CVipData & sVipData = stBaseInfo._stVipData;
    int _uiGid = stBaseInfo._uiGID;

    stAns.set_gid( _uiGid );
    stAns.set_gift_type( iGiftType );
    stAns.set_ret( RET_OK );

    if( iGiftType==CONST_VIP_DAILY_GIFT)//每日礼包
    {
        int iLeftCounts = sVipData._sVipDailyAward.Get( CMainCtrl::m_pstMain->m_iNow, 0 );
        MsgGiftStatusOne* mGiftOne = stAns.add_gift_status();

        mGiftOne->set_id(0);

        if( iLeftCounts<1 )
        {
            mGiftOne->set_status(CONST_VIP_GIFT_OPENED);
        }
        else
        {
            mGiftOne->set_status(CONST_VIP_GIFT_CAN_OPEN);
        }
    }
    else if( iGiftType==CONST_VIP_DAILY_GIFT)//等级礼包
    {
        for( int i=1; i<MAX_VIP_LEVEL; ++i )
        {
            MsgGiftStatusOne* mGiftOne = stAns.add_gift_status();
            mGiftOne->set_id(i);
            mGiftOne->set_status( sVipData._aVipLevelGift[i] );
        }
    }
    else if (CONST_VIP_FIRST_PAY == iGiftType)
    {
        stAns.set_can_get_first_pay_gift(sVipData._ucCanGetFirstPayBonus);
    }

    PBMsgHead  stHead;
    stHead.set_msgret(RET_OK);
    stHead.set_msgtype(CSMsgDef::EMT_ANSWER);
    stHead.set_msgid(ID_MAIN_VIP_GIFT_INFO);

    CMainCtrl::m_pstMain->m_stEncodeBuf.Clear();
    CMainCtrl::m_pstMain->EncodePBMsg(stHead, CMainCtrl::m_pstMain->m_stEncodeBuf);
    CMainCtrl::m_pstMain->EncodePBMsg(stAns, CMainCtrl::m_pstMain->m_stEncodeBuf);
    CMainCtrl::m_pstMain->NotifyOnlineUser(_uiGid, CMainCtrl::m_pstMain->m_stEncodeBuf);
}

int CVipMgr::PrepaidGold(CPlayerBaseInfo& stBaseInfo, CPlayerDetailInfo & stDetailInfo, int iGoldNums, bool bIsYellow)
{
    int iRet = 0;
    if (iGoldNums < 0)
    {
        CT_ERROR(("%d, %d", stBaseInfo._uiGID, iGoldNums));
        return ERROR_VIP_GOLD_NUM;
    }

    bool bHasUpdate = false; //是否需要更新数据入库
    CVipData & sVipData = stBaseInfo._stVipData;

    //10Q = 1金币, 金币数可能小于0, 为了触发首充, 强制设置为1
    if (sVipData._iPayGold <= 0)
    {
        iGoldNums = CT_MAX(1, iGoldNums);
    }

    if (0 == iGoldNums)
    {
        return 0;
    }

    //设置金币
    int iNewGold = sVipData._iPayGold + iGoldNums;

    //设置vip等级
    int iNewVipLevel = 0;
    CVipPayGoldConfig stData;

    if (true)
    {
        for( int i=1; i<MAX_VIP_LEVEL; ++i )
        {
            iRet = this->_stVipConfig.GetVipPayGoldConfig( i, stData );
            if( iRet==RET_OK )
            {
                if( iNewGold<stData._iNeedGold )
                {
                    iNewVipLevel = i-1;
                    break;
                }
                else
                {
                    iNewVipLevel = i;
                }
            }
        }
    }

    if (iNewVipLevel < stBaseInfo._chVipLevel) //有可能直接设置vip等级
    {
        iNewVipLevel = stBaseInfo._chVipLevel;
    }

    if (sVipData._iPayGold <= 0)
    {
        //设置可领取首充奖励
        sVipData._ucCanGetFirstPayBonus = 1;
        NotifyGiftStatus(stBaseInfo, CONST_VIP_FIRST_PAY);
    }

    sVipData._iPayGold = iNewGold;

    //VIP升级后,需要更新通知活动列表
    bool bLevelChanged = false;
    if (iNewVipLevel != stBaseInfo._chVipLevel)
    {
        bLevelChanged = true;
    }

    stBaseInfo._chVipLevel = iNewVipLevel;
    if (bLevelChanged)
    {
        CMainCtrl::m_pstMain->m_stActMgr.NotifyActListToRole(stBaseInfo, stDetailInfo);
    }

    bHasUpdate = true;

    // 入库
    if( bHasUpdate )
    {
        CVarObjectKey stKey;
        stKey.m_chDataType = DT_PLAYER_BASE_INFO;
        stKey.m_u64ID = stBaseInfo._uiGID;
        CMainCtrl::m_pstMain->UpdateData(stKey, stBaseInfo, VOP_UPDATE);
    }

    //推送金币信息
    CMainCtrl::m_pstMain->m_stNotifyMgr.Notify(stBaseInfo._uiGID, NOTIFY_USER_MONEY);

    //推送vip累计金币信息
    if( bHasUpdate )
    {
        NotifyVipInfo( stBaseInfo );
    }

    return iRet;
}

//通知vip信息
void CVipMgr::NotifyVipInfo(CPlayerBaseInfo& stBaseInfo)
{
    MsgAnsVipInfo stAns;

    GetVipInfo(stBaseInfo, stAns);

    PBMsgHead  stHead;
    stHead.set_msgret(RET_OK);
    stHead.set_msgtype(CSMsgDef::EMT_ANSWER);
    stHead.set_msgid(ID_MAIN_GET_VIP_INFO);

    CMainCtrl::m_pstMain->m_stEncodeBuf.Clear();
    CMainCtrl::m_pstMain->EncodePBMsg(stHead, CMainCtrl::m_pstMain->m_stEncodeBuf);
    CMainCtrl::m_pstMain->EncodePBMsg(stAns, CMainCtrl::m_pstMain->m_stEncodeBuf);
    CMainCtrl::m_pstMain->NotifyOnlineUser(stBaseInfo._uiGID, CMainCtrl::m_pstMain->m_stEncodeBuf);
}

int CVipMgr::GetVipDestNum(int iVipLevel, int iModuleId, int &iExtDestNum)
{
    int iRet = 0;
    CVipExtNumConfig stData;

    iRet = this->_stVipConfig.GetVipExtNumConfig(iVipLevel, iModuleId, stData);
    CHECK_RETURN(iRet);

    iExtDestNum = stData._iModuleExtNum;

    return iRet;
}

void CVipMgr::SetYellowDefaultVip(CPlayerBaseInfo& stPlayerBaseInfo)
{
    if (stPlayerBaseInfo._chVipLevel >= MAX_YELLOW_DEFAULT_VIP_LEVEL)
    {
        return;
    }

    stPlayerBaseInfo._chVipLevel = MAX_YELLOW_DEFAULT_VIP_LEVEL;

    //保存数据
    CVarObjectKey stKey;
    stKey.m_chDataType = DT_PLAYER_BASE_INFO;
    stKey.m_u64ID = stPlayerBaseInfo._uiGID;
    CMainCtrl::m_pstMain->UpdateData(stKey, stPlayerBaseInfo, VOP_UPDATE);

    //发送tips
    PBMsgHead  stHead;
    stHead.set_time(CMainCtrl::m_pstMain->m_iNow);
    stHead.set_msgret(RET_OK);
    stHead.set_msgtype(CSMsgDef::EMT_ANSWER);
    stHead.set_msgid(ID_MAIN_TIPS);

    MsgTips stTips;
    stTips.set_type(62);

    CMainCtrl::m_pstMain->m_stEncodeBuf.Clear();
    CMainCtrl::m_pstMain->EncodePBMsg(stHead, CMainCtrl::m_pstMain->m_stEncodeBuf);
    CMainCtrl::m_pstMain->EncodePBMsg(stTips, CMainCtrl::m_pstMain->m_stEncodeBuf);
    CMainCtrl::m_pstMain->NotifyOnlineUser(stPlayerBaseInfo._uiGID, CMainCtrl::m_pstMain->m_stEncodeBuf);

    CMainCtrl::m_pstMain->m_stNotifyMgr.Notify(stPlayerBaseInfo._uiGID, NOTIFY_USER_MONEY);
}

int CVipMgr::GetVipMount (bool bIsYellow, CDropItem& stDropItem)
{
    CDropItemData  stItemData;
    stItemData._ucItemType = ITEM_TYPE_MOUNT;
    stItemData._iItemCount = 1;

    if (bIsYellow)
    {
        stItemData._usItemConfigID = _stYellowGiftConfig._usYellowMount;
    }
    else
    {
        stItemData._usItemConfigID = _stBlueGiftConfig._usBlueMount;
    }

    stDropItem.Clear();
    stDropItem.Drop(stItemData);

    return RET_OK;
}
